﻿<UserControl x:Class="CannedHeater.View.ControlView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:View="clr-namespace:CannedHeater.View"
    xmlns:Converters="clr-namespace:CannedHeater.View.Converters"
    mc:Ignorable="d"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    d:DesignHeight="480" d:DesignWidth="480">

    <!-- Maybe some styles should be added for almost identical controls -->
    
    <!-- DataContext set from MainPage -->
    
    <UserControl.Resources>
        <!-- Kept locally in view code-behind since it is very specific to this view -->
        <View:CmdBtnTextConverter x:Key="cmdBtnTextConverter"/>        
        
        <Converters:ControlModeConverter x:Key="controlModeConverter"/>
        
        <Converters:BoolValidIndicatorToBrushConverter x:Key="boolValidIndicatorToBrushConverter"/>
    </UserControl.Resources>
    
    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>

        <StackPanel Orientation="Horizontal" Grid.Row="0">
            <TextBlock Text="Status: " FontSize="17"/>
            <TextBlock Name="StatusTxtBlock" Text="{Binding StatusMsg}" FontSize="17"/>
        </StackPanel>

        <View:StatusPanelView Grid.Row="1" x:Name="StatusPanel" DataContext="{Binding SensorData}"/>

        <StackPanel Orientation="Vertical" Grid.Row="2">
            <StackPanel Orientation="Horizontal">
                <RadioButton IsChecked="{Binding ControlMode, Converter={StaticResource controlModeConverter}, ConverterParameter=Manual, Mode=TwoWay}"
                             Content="Manual override"
                             x:Name="ManualOverrideBtn" />
                <RadioButton IsChecked="{Binding ControlMode, Converter={StaticResource controlModeConverter}, ConverterParameter=Auto, Mode=TwoWay}"
                             Content="Auto"
                             Margin="30,0,0,0"/>
            </StackPanel>
            <Border Margin="35,0,0,0" BorderBrush="White" BorderThickness="2">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition/>
                        <RowDefinition/>
                        <RowDefinition/>
                    </Grid.RowDefinitions>

                    <TextBlock Margin="20" Text="Speed:" VerticalAlignment="Center" Grid.Row="0"/>
                    <RadioButton IsChecked="{Binding ControlMode, Converter={StaticResource controlModeConverter}, ConverterParameter=Time, Mode=TwoWay}"
                                 IsEnabled="{Binding ElementName=ManualOverrideBtn, Path=IsChecked}"
                                 Content="Time:"
                                 VerticalAlignment="Center"
                                 Grid.Row="1"
                                 FontSize="20"/>
                    <RadioButton IsChecked="{Binding ControlMode, Converter={StaticResource controlModeConverter}, ConverterParameter=Temp, Mode=TwoWay}"
                                 IsEnabled="{Binding ElementName=ManualOverrideBtn, Path=IsChecked}" 
                                 Content="Until temp. is:"
                                 VerticalAlignment="Center"
                                 Grid.Row="2"
                                 FontSize="20"/>

                    <!-- Note the foreground binding to set red foreground color in case of invalid value
                         Ideally NotifyOnValidationError=True, ValidatesOnExceptions=True should have been used,
                         however exceptions are going 'unhandled' even with ValidatesOnExceptions=True.
                         Might be a simple workaround, but using a converter this way is also simple, and works -->
                    
                    
                    <TextBox IsEnabled="{Binding ControlMode, Converter={StaticResource controlModeConverter}, ConverterParameter=Manual}"
                             Text="{Binding ControlSpeedTxt, Mode=TwoWay}"
                             Foreground="{Binding ControlSpeedValid, Converter={StaticResource boolValidIndicatorToBrushConverter}, ConverterParameter={StaticResource PhoneTextBoxForegroundBrush}}"
                             HorizontalAlignment="Center"
                             Width="100"
                             TextAlignment="Center"
                             Grid.Row="0"
                             Grid.Column="1"
                             FontSize="20"
                             VerticalAlignment="Center">
                        <TextBox.InputScope>
                            <InputScope>
                                <InputScopeName NameValue="Digits"/>
                            </InputScope>
                        </TextBox.InputScope>
                    </TextBox>

                    <TextBox IsEnabled="{Binding ControlMode, Converter={StaticResource controlModeConverter}, ConverterParameter=Time}"
                             Text="{Binding ControlTimeTxt, Mode=TwoWay}"
                             Foreground="{Binding ControlTimeValid, Converter={StaticResource boolValidIndicatorToBrushConverter}, ConverterParameter={StaticResource PhoneTextBoxForegroundBrush}}"
                             HorizontalAlignment="Center"
                             Width="100"
                             TextAlignment="Center"
                             Grid.Row="1"
                             Grid.Column="1"
                             FontSize="20"
                             VerticalAlignment="Center">
                        <TextBox.InputScope>
                            <InputScope>
                                <InputScopeName NameValue="Digits"/>
                            </InputScope>
                        </TextBox.InputScope>
                    </TextBox>
                    
                    <TextBox IsEnabled="{Binding ControlMode, Converter={StaticResource controlModeConverter}, ConverterParameter=Temp}"
                             Text="{Binding ControlTempTxt, Mode=TwoWay}"
                             Foreground="{Binding ControlTempValid, Converter={StaticResource boolValidIndicatorToBrushConverter}, ConverterParameter={StaticResource PhoneTextBoxForegroundBrush}}"
                             HorizontalAlignment="Center"
                             Width="100"
                             TextAlignment="Center"
                             Grid.Row="2"
                             Grid.Column="1"
                             FontSize="20"
                             VerticalAlignment="Center">
                        <TextBox.InputScope>
                            <InputScope>
                                <InputScopeName NameValue="Digits"/>
                            </InputScope>
                        </TextBox.InputScope>
                    </TextBox>
                </Grid>
            </Border>
        </StackPanel>


        <Grid Grid.Row="3">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Button Grid.Column="0" FontSize="20"
                    Content="{Binding Connected, Converter={StaticResource cmdBtnTextConverter}}"
                    Click="ConnectSendCmdBtn_Click"/>
            <Button Grid.Column="1" Name="SettingsBtn" FontSize="20"
                    Click="SettingsBtn_Click">
                <StackPanel Orientation="Horizontal" VerticalAlignment="Center">
                    <Image Source="Images/SettingsIcon.png" Stretch="Uniform" Height="40" VerticalAlignment="Center"/>
                    <TextBlock Text="Settings" FontSize="20" VerticalAlignment="Center"/>
                </StackPanel>
            </Button>
        </Grid>

    </Grid>
</UserControl>
